package com.link.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import com.link.inout.AuthenticationOutput;
import com.link.po.Account;
import com.others.util.CookieUtils;

public class LoginAction extends BaseAction{
    
    private static final long serialVersionUID = -4775933495436838278L;

    public String input () {
        if (getAccount() != null) {
            getAccount().setName(STRING_EMPTY);
        }
        return INPUT;
    }
    
    public String login () {


        if(getAccount() != null) {
            Account accountInput = getAccount();
            
            //By Default
            accountInput.setAddDefaultFlag(true);
            
            if (accountInput.getPassword() != null) {
                accountInput.setPassword(accountInput.getPassword().trim());
            }
            
            if(accountInput.isGroupFlg()) {
                //Empty.
            } else {
                if (accountInput.getName() != null) {
                    accountInput.setName(accountInput.getName().trim());
                }
            }

                AuthenticationOutput authenOutput = getAuthenticationService().verifyAccount(accountInput);
                Account account = authenOutput.getAccount();

                // If account is null, of course. it is Invalid.
                if (account != null && authenOutput.isValidFlg()) {
                    if (autoLoginFlag) {
                        Cookie cookie = cookieUtils.addCookie(account);
                        getResponse().addCookie(cookie); // add cookie to
                                                         // response.
                    }
                    // add account into session.
                    getSession().put(ACCOUNT_SESSION, account);
                    setAccount(account);
                    return LOGIN;
                } else {
                    List<Object> args = new ArrayList<Object>();
                    args.add(accountInput.getName());
                    addActionError(getText(ERROR_INVALID_PASSWORD, args));
                    setLoginFailedFlag(true);
                    return FAILED;
                }
            
            
        }

            addActionError(getText(ERROR_USERNAME_PASSWORD_EMPTY));
            setLoginFailedFlag(true);
            return FAILED;   

    }

    public String logout() {
      if (getSession() != null) {
          getSession().remove(ACCOUNT_SESSION);  
        }
        Cookie cookie = cookieUtils.deleteCookie(getRequest());  
        if (cookie != null)  {
            getResponse().addCookie(cookie);  
        }
        return LOGOUT;
    }
    
    private boolean autoLoginFlag;
    private CookieUtils cookieUtils = new CookieUtils();

    public boolean isAutoLoginFlag() {
        return autoLoginFlag;
    }

    public void setAutoLoginFlag(boolean autoLoginFlag) {
        this.autoLoginFlag = autoLoginFlag;
    }
}